package Control;

import java.lang.Math;
import java.util.*;



public class MonteCarlo{
	private double dt, st;
	private double Sum, Drift, vSqrdt;
	private int i, j, z;
	
	public MonteCarlo(double dt, double st, double Sum, double Drift, double vSqrdt, int i, int j, int z){
		this.dt=dt;	
		this.st=st;
		this.Sum=Sum;
		this.Drift=Drift;
		this.vSqrdt=vSqrdt;
		this.i=i;
		this.j=j;
		this.z=z;
	}
	
	public double MonteCarlo(String CallPutFlag, double S, double X, double T, double r, double b, double v, int nSteps, int nSimulations){
		Random rnd = new Random();
		double temp;
		double valfin;
		long startTime;
		long endTime;
		long Time;
		
		startTime = System.currentTimeMillis();
		dt = T / nSteps;
		//System.out.println(dt);
		temp = v * v;
		//System.out.println(temp);
		Drift = (b - temp / 2) * dt;
		//System.out.println(Drift);
		vSqrdt = v * Math.sqrt(dt);
		//System.out.println(vSqrdt);
		if(CallPutFlag.compareTo("c")==0){
			z = 1;
		}else{
			z = -1;
		}
		for(i=1;i<nSimulations;i++){
			st = S;
			for(j=1;j<nSteps;j++){
				st = st * Math.exp(Drift + vSqrdt * rnd.nextGaussian());
				//System.out.println(st);
				//System.out.println("toto "+ j);
			}
			Sum = Sum + Math.max(z * (st - X), 0);
			//System.out.println(Sum);
		}
		valfin = Math.exp(-r * T) * (Sum / nSimulations);
		System.out.println("MonteCarlo: "+ valfin);
		endTime = System.currentTimeMillis();
		Time = endTime - startTime;
		System.out.println("execution time: "+ Time);
		return valfin;
	}
	

}
